package studentsinfomanagement.view.student;


import studentsinfomanagement.bean.Course;
import studentsinfomanagement.bean.SC;
import studentsinfomanagement.bean.Student;
import studentsinfomanagement.dao.CourseDAO;
import studentsinfomanagement.dao.SCDAO;
import studentsinfomanagement.dao.TeacherDAO;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 * 学生选课中心

 */
public class StudentCourseCenter extends JFrame {

    private StudentCourseCenter studentCourseCenter = null;
    private Student student = null;//学生信息对象
    private DefaultTableModel tableModel = null;
    private JTable table = null;//我的选课
    private volatile int state;//标识在哪一个界面 0-我的选课 1-全部课程

    {
        table = new JTable();
    }

    public StudentCourseCenter(Student student){
        studentCourseCenter = this;
        this.student = student;
        this.setTitle("学生选课中心");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //界面元素
        //课程信息显示区上方菜单
        JMenuBar jmb = new JMenuBar();
        JMenu jMenu1 = new JMenu("我的选课");
        JMenu jMenu2 = new JMenu("全部课程");
        jmb.add(jMenu1);
        jmb.add(jMenu2);

        //课程滚动显示区域
        replaceWithMyCourses();
        JScrollPane scrollPane = new JScrollPane(table);

        //退出选课中心
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.setJMenuBar(jmb);
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);

        //为栏目添加监听器
        //我的选课
        jMenu1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                replaceWithMyCourses();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //全部课程
        jMenu2.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                replaceWithAllCourses();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //退出按钮
        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                studentCourseCenter.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //全部课程表格的行选择监听器   选课
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (state == 1) {//只显示在全部课程界面
                    //获取选择行的课程名称
                    int sel_row = table.getSelectedRow();
                    String cno = (String) table.getValueAt(sel_row, 0);
                    //询问用户是否确认选课
                    int value = JOptionPane.showConfirmDialog(studentCourseCenter, "确认选课？");
                    if (value == JOptionPane.YES_OPTION) {//学生确认选课
                        SCDAO scdao = SCDAO.getInstance();//获取选课数据层对象
                        boolean result = scdao.selectCourse(student.getSno(), cno);//学生选修课程
                        if (!result) {//选课失败
                            JOptionPane.showMessageDialog(studentCourseCenter, "选课失败！");
                        } else {//选课成功
                            JOptionPane.showMessageDialog(studentCourseCenter, "选课成功！");
                        }
                    }
                }
            }
        });


    }





/**
     * 显示我的选课
     * @return 滚动显示区域
     */
    public void replaceWithMyCourses(){
        state = 0;
        //课程滚动显示区域

        String[] columnNames = new String[]{"课程名","授课老师","成绩"};  // 列名
        String [][]tableVales= null; //数据
        //查询选课表、课程表、教师表
        SCDAO scdao = SCDAO.getInstance();
        CourseDAO courseDAO = CourseDAO.getInstance();
        TeacherDAO teacherDAO = TeacherDAO.getInstance();
        List<SC> scList = scdao.getSelectedCourseBySno(student.getSno());//获得选课列表
        tableVales = new String[scList.size()][];
        int i = 0;
        for(SC sc:scList){
            Course course = courseDAO.getCourseByCno(sc.getCno());
            //选课课程名
            String cName = course.getName();
            //授课老师名
            String tName = teacherDAO.getTeacherByTno(course.getTno()).getName();
            //成绩
            Float grade = sc.getGrade();
            //创建二维空间
            tableVales[i] = new String[3];
            tableVales[i][0] = cName;
            tableVales[i][1] = tName;
            tableVales[i][2] = grade.toString();
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(tableModel);
        revalidate();
    }

    /**
     * 显示全部课程
     */
    public void replaceWithAllCourses(){
        state = 1;
        //课程滚动显示区域
        // 列名
        String[] columnNames = new String[]{"课程号","课程名","授课老师","学分"};
        String [][]tableVales= null; //数据
        //查询课程表、教师表
        CourseDAO courseDAO = CourseDAO.getInstance();
        TeacherDAO teacherDAO = TeacherDAO.getInstance();
        List<Course> courseList = courseDAO.getCourses();
        tableVales = new String[courseList.size()][];
        int i = 0;
        for(Course c:courseList){

            Course course = courseDAO.getCourseByCno(c.getCno());
            //课程号
            String cno = course.getCno();
            //选课课程名
            String cName = course.getName();
            //授课老师名
            String tName = teacherDAO.getTeacherByTno(course.getTno()).getName();
            //学分
            Integer score = course.getScore();
            //创建二维空间
            tableVales[i] = new String[4];
            tableVales[i][0] = cno;
            tableVales[i][1] = cName;
            tableVales[i][2] = tName;
            tableVales[i][3] = score.toString();
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(tableModel);
        revalidate();
    }


}
